﻿using System;
using System.Windows.Forms;
using PMIS.BLL;
using PMIS.BLL.User;

namespace PMIS.UI.User
{
	/// <summary>
	/// 学历
	/// </summary>
	public partial class 学历信息维护 : BaseForm
	{
		#region 私有变量
		/// <summary>
		/// 当前操作的记录
		/// </summary>
		private 学历 CurrentRecord = null;
		#endregion

		#region 单例
		/// <summary>
		/// 本类的实例
		/// </summary>
		private static 学历信息维护 _form = null;
		/// <summary>
		/// 用于本类实现单例的Locker
		/// </summary>
		private static object locker = new object();
		/// <summary>
		/// 构造函数
		/// </summary>
		private 学历信息维护()
		{
			InitializeComponent();
		}
		/// <summary>
		/// 获取本窗体的实例
		/// </summary>
		/// <param name="ID">记录ID</param>
		/// <param name="strUserID">用户ID</param>
		/// <returns></returns>
		public static 学历信息维护 GetInstance(string ID, string strUserID)
		{
			if (_form == null || _form.IsDisposed)
			{
				lock (locker)
				{
					if (_form == null || _form.IsDisposed)
						_form = new 学历信息维护();
				}
			}

			if (ID == "new")
			{
				_form.CurrentRecord = new 学历();
				_form.CurrentRecord.毕业时间 = DateTime.Now;
				_form.CurrentRecord.USER_ID = strUserID;
			}
			else
			{
				_form.CurrentRecord = Singleton<BL_学历>.GetInstance().GetEntity(ID);
			}

			return _form;
		}
		#endregion

		#region 窗体事件
		/// <summary>
		///	窗体载入事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void UserEducationOperationForm_Load(object sender, EventArgs e)
		{
			UpdateFormView();
		} 
		#endregion

		#region 界面显示
		/// <summary>
		/// 更新界面显示
		/// </summary>
		private void UpdateFormView()
		{
			if (CurrentRecord == null)
				return;

			try
			{
				//	毕业时间
				dtpEducated.Value = CurrentRecord.毕业时间;
				//	学习类别
				tbType.Text = CurrentRecord.学习类别;
				//	毕业学校
				tbSchool.Text = CurrentRecord.毕业学校;
				//	专业
				tbProfessional.Text = CurrentRecord.专业;
				//	毕业证书号
				tbCertificateNo.Text = CurrentRecord.毕业证书号;
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		#endregion

		#region 触发事件
		/// <summary>
		/// 检查输入
		/// </summary>
		/// <returns></returns>
		private bool CheckInput()
		{
			if (string.IsNullOrEmpty(tbType.Text.Trim()))
			{
				ShowErrorMessage("学习类别不能为空!");
				tbType.Focus();
				return false;
			}

			if (string.IsNullOrEmpty(tbSchool.Text.Trim()))
			{
				ShowErrorMessage("毕业学校不能为空!");
				tbSchool.Focus();
				return false;
			}

			if (string.IsNullOrEmpty(tbProfessional.Text.Trim()))
			{
				ShowErrorMessage("专业不能为空!");
				tbProfessional.Focus();
				return false;
			}

			if (string.IsNullOrEmpty(tbCertificateNo.Text.Trim()))
			{
				ShowErrorMessage("毕业证书号不能为空!");
				tbCertificateNo.Focus();
				return false;
			}

			return true;
		}
		/// <summary>
		/// 保存
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btSave_Click(object sender, EventArgs e)
		{
			if (!CheckInput())
				return;

			try
			{
				//	毕业时间
				CurrentRecord.毕业时间 = dtpEducated.Value;
				//	学习类别
				CurrentRecord.学习类别 = tbType.Text.Trim();
				//	毕业院校
				CurrentRecord.毕业学校 = tbSchool.Text.Trim();
				//	专业
				CurrentRecord.专业 = tbProfessional.Text.Trim();
				//	毕业证书号
				CurrentRecord.毕业证书号 = tbCertificateNo.Text.Trim();

				//	保存
				if (Singleton<BL_学历>.GetInstance().Save(CurrentRecord))
				{
					ShowSuccessMessage("保存成功!");
					DialogResult = DialogResult.OK;
					this.Close();
				}
				else
				{
					ShowErrorMessage("保存记录时发生错误!");
					DialogResult = DialogResult.Cancel;
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 取消
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btCancel_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
			this.Close();
		}
		#endregion
	}
}